package com.xoa.service.certificate;

import com.alibaba.fastjson.JSON;
import com.xoa.dao.certificate.CrmCertificateMapper;
import com.xoa.global.timedTask.CertificateTime;
import com.xoa.model.certificate.CrmCertificateWithBLOBs;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *
 * @作者 李卫卫
 * @创建日期 9:05 2020/8/7
 * @类介绍 添加资源证书
 * @参数   attachmentId 证件照 id串  attachmentName证件照 名称串
 * @return limsJson
 */

@Service
public class CrmCertificateService {

    @Autowired
    private CrmCertificateMapper crmCertificateMapper;

    @Resource
    private CertificateTime certificateTime;

    /**
     *
     * @作者 李卫卫
     * @创建日期 22:13 2020/8/10
     * @类介绍 加资源证书
     * @参数
     * @return limsJson
     */

    public ToJson insetCertificate (HttpServletRequest request , String json){
        ToJson toJson = new ToJson(1,"err");
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            String sqlType =  (String) request.getSession().getAttribute("loginDateSouse");

            List<CrmCertificateWithBLOBs> dataArr = JSON.parseArray(json,CrmCertificateWithBLOBs.class);


            //json数组对象 转为  list集合对象
            //List<CrmCertificateWithBLOBs> lists= new Gson().fromJson(json, new TypeToken<List<CrmCertificateWithBLOBs>>() {}.getType());
            for (CrmCertificateWithBLOBs certificateWithBLOBs : dataArr) {
                certificateWithBLOBs.setSupplierUser(user.getUserId());
                crmCertificateMapper.insertSelective(certificateWithBLOBs);

                //定时任务
                certificateTime.CerticateTo(certificateWithBLOBs,sqlType,true);
            }

            toJson.setMsg("ok");
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }


    /**
     * 删除接口
     * @param certId
     * @return
     */
    public ToJson deleteCertificate (Integer certId){
        ToJson toJson = new ToJson(1,"err");
        int count = crmCertificateMapper.deleteByPrimaryKey(certId);
        if(count>0){
            toJson.setCode("0");
            toJson.setMsg("删除成功");
        }
        return toJson;
    }

    /**
     *
     * @作者 李卫卫
     * @创建日期 14:51 2020/8/7
     * @类介绍 模糊查询 以及页面展示数据
     * @参数
     * @return limsJson
     */

    public ToJson selectByCertificates(HttpServletRequest request , CrmCertificateWithBLOBs crmCertificateWithBLOBs , Integer page , Integer limit){
        ToJson toJson = new ToJson(1,"err");
        Map map = new HashMap();

        try {
            PageParams pageParams = new PageParams();
            pageParams.setPageSize(limit);
            pageParams.setPage(page);
            pageParams.setUseFlag(true);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            String sqlType = "xoa" + (String) request.getSession().getAttribute("loginDateSouse");
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);

            //当前登录人
            map.put("userId",user.getUserId());

            //登陆人角色  (如果角色不是供应商  就查看  全部  如果是供应商  就只能看自己的)
            map.put("userPriv",user.getUserPriv());

            //主键ID
            map.put("certId",crmCertificateWithBLOBs.getCertId());

            //证书状态 等于空  就是 查询全部证书 %2C
            if (!StringUtils.checkNull(crmCertificateWithBLOBs.getStatus())){
                map.put("status",crmCertificateWithBLOBs.getStatus());

                //获取后三十天
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DAY_OF_MONTH, 31);
                Date date = cal.getTime();
                map.put("willDate",DateFormat.getDatestr(date));

                switch (crmCertificateWithBLOBs.getStatus()){
                    case "1":
                        //获取后1天
                        Calendar cal2 = Calendar.getInstance();
                        cal2.add(Calendar.DAY_OF_MONTH, 1);
                        Date date2 = cal2.getTime();
                        map.put("currentDate",DateFormat.getDatestr(date2));
                        break;
                    case "2":
                        map.put("currentDate",DateFormat.getDatestr(new Date()));
                        break;
                }
            }


            //搜索
            map.put("certName",crmCertificateWithBLOBs.getCertName());

            map.put("lawName",crmCertificateWithBLOBs.getLawName());

            List<CrmCertificateWithBLOBs> selectByCertificates = crmCertificateMapper.selectByCertificates(map);
            for (CrmCertificateWithBLOBs certificates: selectByCertificates) {

                //附件
                if (!StringUtils.checkNull(certificates.getAttachmentName()) && !StringUtils.checkNull(certificates.getAttachmentId())) {
                    List<Attachment> institution = GetAttachmentListUtil.returnAttachment(certificates.getAttachmentName(), certificates.getAttachmentId(), sqlType, "crmCertificate");
                    certificates.setAttachmentList(institution);
                }

            }

            toJson.setObj(selectByCertificates);
            PageParams pageParams = (PageParams) map.get("pageParams");
            toJson.setTotleNum(pageParams.getTotal());
            toJson.setFlag(0);
            toJson.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
        }
        return toJson;
    }

    //条件查询
    public List<CrmCertificateWithBLOBs> selectByCertificates(Map map){
        return crmCertificateMapper.selectByCertificates(map);
    }



    /**
     *
     * @作者 李卫卫
     * @创建日期 17:04 2020/8/10
     * @类介绍 编辑资源证书
     * @参数
     * @return limsJson
     */
    public ToJson updateByPrimaryKey (CrmCertificateWithBLOBs crmCertificate){
        ToJson toJson = new ToJson(1,"err");
        try {
            if(crmCertificate!=null){
                int count = crmCertificateMapper.updateByPrimaryKeySelective(crmCertificate);
                if(count>0){
                    toJson.setCode("0");
                    toJson.setMsg("修改成功");
                }else {
                    toJson.setMsg("修改失败");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return toJson;
    }



}
